﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;

namespace Black_Jack_Basic_Strategy
{
    public partial class FormBlackJack : Form
    {
        RandomizedKeySet rksHand = new RandomizedKeySet();
        Deck m_deck;
        Player m_Dealer = new Player();
        Player m_Player = new Player();
        private const int MIN_CARDS_IN_DECK = 30;       //This is for the re-shuffle limit.
        private const int NUM_OF_DECKS = 6;
        public const int BUST_VALUE = 21;
        private FlashCards m_FlashCard;
        private Explanations m_Explanation;
        public FormBlackJack()
        {
            InitializeComponent();
        }

        private void FormBlackJack_Load(object sender, EventArgs e)
        {
            labelDealerHand.Text = "";
            lblPlayerHandTotal.Text = "";
            labelPlayerHand.Text = "";
            labelPlayerAction.Text = "";
            textBoxExplanation.Text = "";

            m_Dealer.IsDealer = true;
            m_Player.IsDealer = false;

            m_deck = new Deck(NUM_OF_DECKS);
            m_deck.Shuffle();
            Card BurnCard = m_deck.GetNextCard();
           
            ConnectToSqlServer();
            NextHand();     //Fix null object references.
            PlayerTests pt = new PlayerTests();
            pt.TestTwoAces();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (this.radioButtonToughHands.Checked)
            {
                HandEvaluator he;
                do
                {
                    he = new HandEvaluator(m_Dealer, m_Player);
                    NextHand();
                } while (he.IsToughHand() == false);
            }
            else
                NextHand();
        }

        private bool ShouldSaveToDB()
        {
            if (this.cbSaveToDB.Checked)
                return true;
            else
                return false;
        }

        private void NextHand()
        {
            //Reset the explain box:
            this.textBoxExplanation.Text = "";

            //Check to see if the deck needs to be shuffled:
            if (m_deck.NeedsShuffle(MIN_CARDS_IN_DECK))
            {
                this.textBoxExplanation.Text = "Shuffling....";
                m_deck.Shuffle();
                m_deck.GetNextCard();       //Burn card.
            }
            //Reset the Player and Dealer hands:
            m_Player.m_Hand.Clear();
            m_Dealer.m_Hand.Clear();

            //Deal a card to the player, then dealer, then player then dealer.
            m_Player.m_Hand.Add(m_deck.GetNextCard());
            m_Dealer.m_Hand.Add(m_deck.GetNextCard());
            m_Player.m_Hand.Add(m_deck.GetNextCard());
            m_Dealer.m_Hand.Add(m_deck.GetNextCard());

            labelDealerHand.Text = m_Dealer.GetHandString();
            labelPlayerHand.Text = m_Player.GetHandString();
            lblPlayerHandTotal.Text = m_Player.GetHandTotal().ToString();
            labelPlayerAction.Text = "";

            //Get the suggested move for this hand pair (if it exists in DB):
            DataAccess.FlashCards fc = new DataAccess.FlashCards();
            m_FlashCard = fc.GetCard(m_Dealer.GetDealerHandDBValue(), m_Player.GetPlayerHandDBValue());
            DataAccess.Explanations e = new DataAccess.Explanations(m_FlashCard.ExplanationID);
            m_Explanation = e;
            if (ShouldSaveToDB())
                this.textBoxExplanation.Text = e.ExplanationText;

        }

        private void SaveToDB(HandEvaluator.HandAction action)
        {
            FlashCards fc = new FlashCards();
            Explanations e = new Explanations();

            e.ExplanationText = this.textBoxExplanation.Text;
            if (e.ExplanationText == "")
            {
                MessageBox.Show("Can not save to database - need an explantion.");
                return;
            }
            e.SaveOrGetIDByText();
            fc = fc.GetCard(m_Dealer.GetDealerHandDBValue(), m_Player.GetPlayerHandDBValue());       //Prevents dupelicate hands.
            fc.ExplanationID = e.ExplanationID;
            fc.DealersUpCard = m_Dealer.GetDealerHandDBValue();
            fc.PlayersHand = m_Player.GetPlayerHandDBValue();
            fc.SuggestedActionID = (int)action;
            fc.ToughHand = this.radioButtonToughHands.Checked;
            fc.Save();
            this.Status.Text = "Saved to Database.";
        }

        private void buttonHit_Click(object sender, EventArgs e)
        {
            labelPlayerAction.Text = "Hit";
            if (m_FlashCard.SuggestedActionID == (int)HandEvaluator.HandAction.ShouldHit)
                labelPlayerAction.ForeColor = Color.Blue;
            else
                labelPlayerAction.ForeColor = Color.Red;
            if (ShouldSaveToDB())
                SaveToDB(HandEvaluator.HandAction.ShouldHit);
            else 
                textBoxExplanation.Text = m_Explanation.ExplanationText;
        }

        private void buttonStand_Click(object sender, EventArgs e)
        {
            labelPlayerAction.Text = "Stand";
            if (m_FlashCard.SuggestedActionID == (int)HandEvaluator.HandAction.ShouldStay)
                labelPlayerAction.ForeColor = Color.Blue;
            else
                labelPlayerAction.ForeColor = Color.Red;
            if (ShouldSaveToDB())
                SaveToDB(HandEvaluator.HandAction.ShouldStay);
            else
                textBoxExplanation.Text = m_Explanation.ExplanationText;
        }

        private void buttonDoubleDown_Click(object sender, EventArgs e)
        {
            labelPlayerAction.Text = "Double Down";
            if (m_FlashCard.SuggestedActionID == (int)HandEvaluator.HandAction.ShouldDouble)
                labelPlayerAction.ForeColor = Color.Blue;
            else
                labelPlayerAction.ForeColor = Color.Red;
            if (ShouldSaveToDB())
                SaveToDB(HandEvaluator.HandAction.ShouldDouble);
            else
                textBoxExplanation.Text = m_Explanation.ExplanationText;
        }

        private void buttonSplit_Click(object sender, EventArgs e)
        {
            labelPlayerAction.Text = "Split";
            if (m_FlashCard.SuggestedActionID == (int)HandEvaluator.HandAction.ShouldSplit)
                labelPlayerAction.ForeColor = Color.Blue;
            else
                labelPlayerAction.ForeColor = Color.Red;
            if (ShouldSaveToDB())
                SaveToDB(HandEvaluator.HandAction.ShouldSplit);
            else
                textBoxExplanation.Text = m_Explanation.ExplanationText;
        
        }

        private void buttonShuffle_Click(object sender, EventArgs e)
        {
            // TODO Get Set count.
            //handsArray = rksHand.ShuffleSet(5);
            m_deck.Shuffle();
            m_deck.GetNextCard();       //Burn a card.
        }

        public void ConnectToSqlServer()
        {
            // TODO Find path function.
            //string strPath = @"C:\Users\Kermit\Documents\Visual Studio 2008\Projects\Black Jack Basic Strategy\Black Jack Basic Strategy\BlackJackBasicStategy.mdb";
          
            try
            {
                System.Data.SqlClient.SqlConnection dbConn = new System.Data.SqlClient.SqlConnection(DataAccess.TheSettings.SQLConnectionString);
                dbConn.Open();                
                this.textBoxExplanation.Text = "DB opened";
                // Insert code to process data.
                dbConn.Close();
            }
            catch (Exception ex)
            {
                this.textBoxExplanation.Text = ex.ToString();
            }
            finally
            {
            }
        }


    }
}
